import 'package:flutter/material.dart';
import 'dart:ui';

//使屏幕宽度假定为750
class Adapt {
  static const double designWidth = 750;

  static MediaQueryData mediaQuery = MediaQueryData.fromWindow(window);
  static double _width = mediaQuery.size.width;
  static double _height = mediaQuery.size.height;
  static double _topbarH = mediaQuery.padding.top;
  static double _botbarH = mediaQuery.padding.bottom;
  static double _pixelRatio = mediaQuery.devicePixelRatio;
  static double _ratio = 0.55;
  static bool isInit = false;

  static loadMediaQueryData() {
    if (!isInit) {
      mediaQuery = MediaQueryData.fromWindow(window);
      _width = mediaQuery.size.width;
      _width = double.parse(_width.toStringAsFixed(2));
      if (_height / _width < 1.5) {
        Adapt.px(750 * 1.6 / (_height / _width));
      } else {
        Adapt.init(750);
      }
      isInit = true;
    }
  }

  static init(int number) {
    int uiwidth = number is int ? number : 750;
    _ratio = _width / uiwidth;
    _ratio = double.parse(_ratio.toStringAsFixed(2));
    print('_ratio : ${_ratio.toString()} _width: ${_ratio.toString()} ');
  }

  static double px(number) {
    return number * _ratio;
  }

  static onepx() {
    return 1 / _pixelRatio;
  }

  static screenW() {
    return _width;
  }

  static screenH() {
    return _height;
  }

  static padTopH() {
    return _topbarH;
  }

  static padBotH() {
    return _botbarH;
  }
}
